package org.zhxy.core.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.zhxy.common.config.MapperFactory;
import org.zhxy.common.response.Result;
import org.zhxy.common.response.ResultCode;
import org.zhxy.common.utils.CollectionUtils;
import org.zhxy.common.utils.Constants;
import org.zhxy.core.system.mapper.PermissionMapper;
import org.zhxy.core.system.mapper.RoleMapper;
import org.zhxy.entity.system.Permission;
import org.zhxy.entity.system.Role;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PermissionService {

    public List<Permission> getPermissionListByUserId(String userId) {
        return MapperFactory.permissionMapper.getPermissionListByUserId(userId);
    }

    public List<Permission> findAll() {
        return MapperFactory.permissionMapper.selectList(null);
    }

    public List<Permission> findAllByCasCader(Map<String, Object> map) {
        List<Permission> permissions = this.getPermissions(Constants.CharacterNumber.ZERO);
        List<Permission> returnList = new ArrayList<>();;
        if(CollectionUtils.checkCollection(permissions)){
            for (Permission permission : permissions) {
                if(!StringUtils.isEmpty(map.get(Constants.DB.NAME))){
                    if(permission.getName().contains(map.get(Constants.DB.NAME).toString())){
                        returnList.add(permission);
                    }
                }
            }
        }
        if(!StringUtils.isEmpty(map.get(Constants.DB.NAME))) return returnList;
        else return permissions;
    }

    protected List<Permission> getPermissions(String pid) {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Constants.DB.PID,pid);
        List<Permission> permissionList = MapperFactory.permissionMapper.selectList(queryWrapper);
        if(CollectionUtils.checkCollection(permissionList)){
            for (Permission permission : permissionList) {
                permission.setChildren(this.getPermissions(permission.getId()));
            }
        }
        return permissionList;
    }

    public Result savePermission(Permission permission) {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Constants.DB.CODE,permission.getCode());
        Permission target = MapperFactory.permissionMapper.selectOne(queryWrapper);
        if(!StringUtils.isEmpty(target)) return new Result(ResultCode.DATA_IS_EXIST);
        QueryWrapper<Permission> permissionQueryWrapper = new QueryWrapper<>();
        permissionQueryWrapper.eq(Constants.DB.PID,permission.getPid());
        permissionQueryWrapper.eq(Constants.DB.TYPE, Constants.CharacterNumber.THREE);
        List<Permission> list = MapperFactory.permissionMapper.selectList(permissionQueryWrapper);
        if(CollectionUtils.checkCollection(list)) return new Result(ResultCode.SYSTEM_ACCESS_PERMISSION_API);
        MapperFactory.permissionMapper.insert(permission);
        return Result.SUCCESS();
    }

    public Result updatePermission(Permission permission) {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Constants.DB.CODE,permission.getCode());
        Permission searchPermission = MapperFactory.permissionMapper.selectOne(queryWrapper);
        if(!StringUtils.isEmpty(searchPermission)) return new Result(ResultCode.DATA_IS_EXIST);
        Permission target = MapperFactory.permissionMapper.selectById(permission.getId());
        BeanUtils.copyProperties(permission,target);
        MapperFactory.permissionMapper.updateById(target);
        return Result.SUCCESS();
    }

    public Result removePermission(String id) {
        List<Role> list = MapperFactory.roleMapper.getRoleListByPermissionId(id);
        if(CollectionUtils.checkCollection(list)) return new Result(ResultCode.SYSTEM_ACCESS_PERMISSION_HAS_ROLE);
        MapperFactory.permissionMapper.deleteById(id);
        return Result.SUCCESS();
    }

    public List<Permission> getPermissionsByRoleId(String roleId) {
        return MapperFactory.permissionMapper.getPermissionListByRoleId(roleId);
    }
}
